package com.qf.kdxf.controller;

import com.qf.kdxf.entity.speechsynthesis.SpeechEntity;
import com.qf.kdxf.service.SpeechSynthesisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

/**
 * @Author 明廷盛
 * @Date 2025/6/11 15:12
 * @Description
 */
@RestController
@RequestMapping("/speechsynthesize")
public class SpeechSynthesisController {
    @Autowired
    SpeechSynthesisService synthesisService;

    /**
     * 01-语音合成
     *
     * @param speechEntity
     * @param request
     * @return
     */
    @RequestMapping("/synthesize")
    @ResponseBody
    public String SpeechSynthesis(@RequestBody SpeechEntity speechEntity,
                                  HttpServletRequest request) {
        String filename = String.valueOf(System.currentTimeMillis());
        try {
            //1.获取鉴权地址
            String wsUrl = synthesisService.getAuthUrl().replace("https://",
                    "wss://");
            //2.文件输出位置
            OutputStream outputStream = new FileOutputStream(
                    request.getServletContext().getRealPath("/") + filename + ".mp3");
            speechEntity.setOutput_file_path(outputStream);
            speechEntity.setWsUrl(wsUrl);
            //3.语音的合成
            synthesisService.websocketWork(speechEntity);
            System.out.println("http://localhost:8080/" + filename + ".mp3");

            //4.输出路径打印
            String outputPath = request.getServletContext().getRealPath("/") + filename + ".mp3";
            String move2Path = "C:\\Users\\15943\\WeChatProjects\\miniprogram-1\\radio\\synthesis.mp3";
            // 是在不行, ①直接返回outputPath ②然后播放这个文件


        } catch (Exception e) {
            e.printStackTrace();
        }
        return "http://localhost:8080/" + filename + ".mp3";
    }
}
